package com.wenjie.config;


import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.wenjie.common.enumHandler.EnumConverterFactory;
import com.wenjie.gateway.CheckInterceptor;
import com.wenjie.login.LoginInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;

@Configuration
public class webconfig implements WebMvcConfigurer {


    /**
     * @description: 跨域问题的处理
     * @author: wenjie
     * @date: 2022/9/21 18:33
     * @param:
     * @return:
     **/
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**").allowCredentials(false).maxAge(3600);
    }

    /**
     * @Description: TODO 添加拦截器
     * @Param:
     * @Return:
     * @Author: wenjie
     * @Date: 2023/1/30
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
//       这个地方应该是/api/**的但是不知道为什么不生效,所以加到CheckInterceptor中了
        registry.addInterceptor(new CheckInterceptor()).addPathPatterns("/**").excludePathPatterns("/api/login").excludePathPatterns("/api/menu/init") // 初始化接口
                .excludePathPatterns("/api/captchaImage");

        registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/**");
    }


    /**
     * 添加转换器，解决long和BigDecimal的数据失真问题
     * 修改前端多传数据后，springmvc报错的问题
     */
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();

        ObjectMapper objectMapper = new ObjectMapper();
        // 空值类型处理
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        // 处理前端数值精度缺失的问题
        SimpleModule simpleModule = new SimpleModule();
        simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
        simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
        simpleModule.addSerializer(BigInteger.class, ToStringSerializer.instance);
        simpleModule.addSerializer(BigDecimal.class, ToStringSerializer.instance);
        objectMapper.registerModule(simpleModule);
        jackson2HttpMessageConverter.setObjectMapper(objectMapper);
        converters.add(6, jackson2HttpMessageConverter);
    }


    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addConverterFactory(new EnumConverterFactory());
    }
}
